//
//  ZFBaseInfoViewController2.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/24.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFBaseInfoViewController2.h"
#import "ZFAuthorizeInfoViewController.h"//信息授权VC
#import "ZFContactViewController.h"//紧急联系人VC
#import "ZFFinanceInfoViewController.h"//财务信息VC
#import "ZFProfessionInfoViewController.h"//职业信息VC
#import "ValuePickerView.h"
#import "ZFHeader.h"

@interface ZFBaseInfoViewController2 ()
{
    NSString *startTime;//进入页面的时间
    
    NSArray *shenfenArray;//社会身份数组
    NSArray *eduArray;//教育程度数组
    NSArray *marrayStateArray;//婚姻状况
    NSArray *houseArray;//住房情况有无
    NSArray *creditArray;//信用卡（）
    
    NSArray *certainlyWriteUIArray;//必填选项UI数组
}
@property (nonatomic,strong) ValuePickerView *pickerView;
@property (nonatomic,strong) AnimatingView *animation;
@property (nonatomic,strong) AlertBoxView *alertV;

@end

@implementation ZFBaseInfoViewController2

//继承父类xib
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:NSStringFromClass([self.superclass class]) bundle:nibBundleOrNil];
//    return self;
//}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"baseInfoView"];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"baseInfoView"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"社会信息";
    [self initData];
}
- (void)initData
{
    startTime = [ZFPulicAPI getCurrentTimestamp];
    
    _animation = [[AnimatingView alloc] init];
    
    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];

    
    certainlyWriteUIArray = @[_userTypeButton,_eduButton,_incomeButton,_contactButton,_chooseCard];
    //初始化数据
    _pickerView = [[ValuePickerView alloc] init];
    
    shenfenArray = @[@"在职人员",@"企业负责人",@"自由职业",@"无业",@"退休",@"个体工商户",@"个体经营者",@"学生"];
    eduArray = @[@"初中以下",@"初中",@"高中/中专/技校",@"专科",@"本科",@"硕士",@"博士及以上"];
    marrayStateArray = @[@"未婚",@"已婚",@"离异/丧偶"];
    houseArray = @[@"自置无按揭",@"自置有按揭",@"亲属楼宇",@"集体宿舍",@"租房",@"自建房",@"共有",@"其他"];
    creditArray = @[@"有信用卡",@"无信用卡"];
}
#pragma mark ———— button的点击事件
- (IBAction)chooseClick:(id)sender {
    
    
    UIButton *button = (UIButton *)sender;
    switch (button.tag) {
        case 1:
            {
                _pickerView.dataSource = shenfenArray;
                _pickerView.pickerTitle = @"社会身份";
                self.pickerView.valueDidSelect = ^(NSString *value) {
                    
                    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                    [button setTitle:stateArr[0] forState:UIControlStateNormal];
                };
                [_pickerView show];
            }
            break;
            
        case 2:
            {
                _pickerView.dataSource = eduArray;
                _pickerView.pickerTitle = @"教育程度";
                self.pickerView.valueDidSelect = ^(NSString *value) {
                    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                    [button setTitle:stateArr[0] forState:UIControlStateNormal];
                };
                [_pickerView show];
            }
            break;
            
        case 3:
            {
                ZFFinanceInfoViewController *financeInfoVC = [ZFFinanceInfoViewController new];
                financeInfoVC.submitStatuBlock = ^(NSInteger submitStatu) {
                    
                    if (submitStatu == 1) {
                        
                        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

                        [button setTitle:@"已填写" forState:UIControlStateNormal];
                        [button setTitleColor:Main_Color1 forState:UIControlStateNormal];
                    }
                };
                [self.navigationController pushViewController:financeInfoVC animated:YES];
            }
            break;
            
        case 4:
            
            {
                ZFContactViewController *contactVC = [ZFContactViewController new];
                contactVC.submitStatuBlock = ^(NSInteger submitStatu) {
                    
                    if (submitStatu == 1) {
                        
                        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                        [button setTitle:@"已填写" forState:UIControlStateNormal];
                        [button setTitleColor:Main_Color1 forState:UIControlStateNormal];
                    }
                };
                [self.navigationController pushViewController:contactVC animated:YES];
            }
            break;
        case 5:
            {
                _pickerView.dataSource = creditArray;
                _pickerView.pickerTitle = @"信用卡";
                self.pickerView.valueDidSelect = ^(NSString *value) {
                    
                    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                    [button setTitle:stateArr[0] forState:UIControlStateNormal];
                };
                [_pickerView show];
            }
            break;
  
        case 6:
            {
                ZFProfessionInfoViewController *professionInfoVC = [ZFProfessionInfoViewController new];
                
                professionInfoVC.submitStatuBlock = ^(NSInteger submitStatu) {
                    
                    if (submitStatu == 1) {
                        
                        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                        [button setTitle:@"已填写" forState:UIControlStateNormal];
                        [button setTitleColor:Main_Color1 forState:UIControlStateNormal];
                    }
                };

                [self.navigationController pushViewController:professionInfoVC animated:YES];
            }
            break;

        case 7:
            {
                _pickerView.dataSource = marrayStateArray;
                _pickerView.pickerTitle = @"婚姻状况";
                self.pickerView.valueDidSelect = ^(NSString *value) {
                    
                    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [button setTitle:value forState:UIControlStateNormal];
                };
                [_pickerView show];

            }
            break;
            
        case 8:
            {
                _pickerView.dataSource = houseArray;
                _pickerView.pickerTitle = @"住房状况";
                self.pickerView.valueDidSelect = ^(NSString *value) {
                    
                    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

                    NSArray * stateArr = [value componentsSeparatedByString:@"/"];
                    [button setTitle:stateArr[0] forState:UIControlStateNormal];
                };
                [_pickerView show];
            
            }

            break;
            
        default:
            break;
    }
}
//提交信息
- (IBAction)nextStep:(id)sender {
    
    if ([self judgeSubmitInfo]) {
    
        ZFUserSocialInfomodal *modal = [ZFUserSocialInfomodal new];
        modal.social_identity = [self getSingleSubmit:shenfenArray writeInfo:_userTypeButton.titleLabel.text];
        modal.edu_level = [self getSingleSubmit:eduArray writeInfo:_eduButton.titleLabel.text];
        modal.startTime = startTime;
        modal.own_credit = [self getSingleSubmit:creditArray writeInfo:_chooseCard.titleLabel.text];
        
        if ([_marriageButton.titleLabel.text rangeOfString:@"请"].location == NSNotFound) {
            
            modal.marry_state = [self getSingleSubmit:marrayStateArray writeInfo:_marriageButton.titleLabel.text];
        }
        else
        {
            modal.marry_state = @"";
        }
        if ([_houseButton.titleLabel.text rangeOfString:@"请"].location == NSNotFound) {
            
            modal.house_state = [self getSingleSubmit:houseArray writeInfo:_houseButton.titleLabel.text];
        }
        else
        {
            modal.house_state = @"";
        }
        
        [_animation startTheSmallAnimation];
        [ZFApplyInfoHttpRequest requestSubmitUserSocialInfo:modal sucessBlock:^(NSDictionary *dict) {
            
            [_animation stopTheSmallAnimation];

            if ([dict[@"is_alert"] integerValue] == 1) {
                
                [_alertV appearAndDisappearAlertWithString:dict[@"msg"]];
            }
            if ([dict[@"code"] integerValue] == 1) {
                
                ZFAuthorizeInfoViewController *authorizeVC = [ZFAuthorizeInfoViewController new];
                authorizeVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:authorizeVC animated:YES];
            }
        } failureBlock:^(NSInteger failSign) {
            
            [_animation stopTheSmallAnimation];
            [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
        }];
    }
}
#pragma mark ———— 判断提交信息是否为空
- (BOOL)judgeSubmitInfo
{
    for (UIButton *button in certainlyWriteUIArray) {
        
        if ([button.titleLabel.text rangeOfString:@"请"].location != NSNotFound) {
            
            [_alertV appearAndDisappearAlertWithString:@"信息不完善"];
            return NO;
        }
    }
    return YES;
}

- (NSString *)getSingleSubmit:(NSArray *)array writeInfo:(NSString *)writeStr
{
    for (int i = 0; i < array.count; i ++) {
        
        if ([array[i] isEqualToString:writeStr]) {
            
            return [NSString stringWithFormat:@"%d",i+1];
        }
    }
    return @"0";
}
#pragma mark ———— 返回
- (BOOL)navigationShouldPopOnBackButton
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [self.navigationController popToRootViewControllerAnimated:YES];
    if (delegate.isFirstEnterApp == YES) {
        
        delegate.isFirstEnterApp = NO;
        return NO;
    }
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
